﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Models;
using BLL;

namespace PCCS.Management.AdminManage
{
    public partial class AdminList : System.Web.UI.Page
    {
        public static string sortName { get; set; }
        public static string sortType { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    sortName = "AdminID";
                    sortType = "asc";
                    BindDataForGridView(sortName, sortType);
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        protected void BindDataForGridView(string sortName, string sortType)
        {
            IList<Admin> admin = AdminManager.GetAllAdminsSort(sortName, sortType);
            gvAdmin.DataSource = admin;
            gvAdmin.DataBind();
        }

        protected void gvAdmin_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LinkButton deleteButton = this.gvAdmin.Rows[e.RowIndex].FindControl("lbDelete") as LinkButton;
            int id = Convert.ToInt32(deleteButton.CommandArgument);
            string adminName = AdminManager.GetAdminByAdminID(id).AdminName;
            AdminManager.DeleteAdminByAdminID(id);

            //日志
            Models.AdminLog log = new Models.AdminLog();
            int logId = Convert.ToInt32(Session["admin"].ToString());
            string logName = AdminManager.GetAdminByAdminID(logId).AdminName;

            log.AdminName = logName;
            log.LogTime = DateTime.Now;
            log.LogDesc = "删除管理员" + adminName + "。";
            AdminLogManager.AddAdminLog(log);

            BindDataForGridView(sortName, sortType);
        }

        protected void gvAdmin_Sorting(object sender, GridViewSortEventArgs e)
        {
            sortName = e.SortExpression;
            if (sortType == "asc")
            {
                sortType = "desc";
            }
            else
            {
                sortType = "asc";
            }

            BindDataForGridView(sortName, sortType);
        }
    }
}
